package com.xtourms.client.rm.ticket;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.json.client.JSONArray;
import com.google.gwt.json.client.JSONObject;
import com.google.gwt.json.client.JSONParser;
import com.google.gwt.json.client.JSONString;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Anchor;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.RadioButton;
import com.google.gwt.user.client.ui.Widget;
import com.xtourms.client.ContentFrame;
import com.xtourms.client.ui.util.MessageBox;
import com.xtourms.shared.field.TicketField;

public class TicketManager extends Composite {

	private static TicketManagerUiBinder uiBinder = GWT
			.create(TicketManagerUiBinder.class);

	interface TicketManagerUiBinder extends UiBinder<Widget, TicketManager> {
	}

	private ContentFrame parent;
	private JSONArray tickets;
	@UiField FlexTable resultTable;
	
	public TicketManager(JSONArray tickets, ContentFrame parent) {
		initWidget(uiBinder.createAndBindUi(this));
		this.tickets = tickets;
		this.parent = parent;
		initTableHeader();
		fill();
	}

	private void initTableHeader(){
		int col = 0;
		resultTable.getCellFormatter().setWidth(0, col, "30px");
		resultTable.setText(0, col++, "选项");
		resultTable.getCellFormatter().setWidth(0, col, "100px");
		resultTable.setText(0, col++, "票务名");
		resultTable.getCellFormatter().setWidth(0, col, "50px");
		resultTable.setText(0, col++, "城市");
		resultTable.setText(0, col++, "地址");
		resultTable.getRowFormatter().addStyleName(0, "result_header");
	}
	
	private void fill(){
		if(null != tickets && tickets.size() > 0){
			for(int index = 0; index < tickets.size(); index++){
				fillRow(index+1, (JSONObject)tickets.get(index));
			}
		}
	}
	
	class detailShow implements ClickHandler {
		
		private int index;
		
		public detailShow(int index){
			this.index = index;
		}
		
		@Override
		public void onClick(ClickEvent event) {
			JSONObject ticketObj = (JSONObject) tickets.get(index);
			TicketDetail detail = new TicketDetail(ticketObj);
			detail.center();
		}
	}
	
	private void fillRow(int row, JSONObject hotel) {
		int col = 0;
		RadioButton rb = new RadioButton("ticket");
		resultTable.setWidget(row, col++, rb);
		String name = ((JSONString)hotel.get(TicketField.TICKET_NAME)).stringValue();
		Anchor hName = new Anchor(name);
		hName.addClickHandler(new detailShow(row-1));
		resultTable.setWidget(row, col++, hName);
		resultTable.setText(row, col++, ((JSONString)hotel.get(TicketField.TICKET_CITY_NAME)).stringValue());
		resultTable.setText(row, col++, ((JSONString)hotel.get(TicketField.TICKET_ADD)).stringValue());
		if(0 == row%2){
			resultTable.getRowFormatter().addStyleName(row, "odd");
		}else{
		 	resultTable.getRowFormatter().addStyleName(row, "even");
		}
	}
	
	@UiHandler("add_ticket")
	void addTicket(ClickEvent evt){
		parent.appendContent(new TicketDetailManager(parent, new AsyncCallback<String>() {
			
			@SuppressWarnings("deprecation")
			@Override
			public void onSuccess(String result) {
				JSONObject hotelObj = (JSONObject) JSONParser.parse(result);
				resultTable.clear();
				initTableHeader();
				fillRow(1, hotelObj);
				parent.resumeContent();
			}
			
			@Override
			public void onFailure(Throwable caught) {
				MessageBox.ins.showError("票务管理服务错误:"+caught.getMessage());
			}
		}));
	}
	
	@UiHandler("modify_ticket")
	void modifyTicket(ClickEvent evt){
		JSONObject ticketObj = null;
		int index = 0;
		if(null != tickets && tickets.size() > 0){
			for(; index < tickets.size(); index++){
				RadioButton rb = (RadioButton)resultTable.getWidget(index+1, 0);
				if(Boolean.TRUE == rb.getValue()){
					ticketObj = (JSONObject)tickets.get(index);
					break;
				}
			}
		}
		if(null != ticketObj){
			final int modifyIndex = index;
			parent.appendContent(new TicketDetailManager(ticketObj, parent, new AsyncCallback<String>() {

				@Override
				public void onFailure(Throwable caught) {
					MessageBox.ins.showError("票务管理服务错误:"+caught.getMessage());
				}

				@SuppressWarnings("deprecation")
				@Override
				public void onSuccess(String ret) {
					JSONObject hotelObj = (JSONObject) JSONParser.parse(ret);
					tickets.set(modifyIndex, hotelObj);
					fillRow(modifyIndex+1, hotelObj);
					parent.resumeContent();
				}
			}));
		}
	}
	
	@UiHandler("manage_status")
	void manageStatus(ClickEvent evt){
		JSONObject ticketObj = null;
		int index = 0;
		if(null != tickets && tickets.size() > 0){
			for(; index < tickets.size(); index++){
				RadioButton rb = (RadioButton)resultTable.getWidget(index+1, 0);
				if(Boolean.TRUE == rb.getValue()){
					ticketObj = (JSONObject)tickets.get(index);
					break;
				}
			}
		}
		final JSONObject selectedTicketObj = ticketObj;
		if(null != selectedTicketObj){
			JSONObject queryObj = new JSONObject();
			queryObj.put(TicketField.TICKET_ID, selectedTicketObj.get(TicketField.TICKET_ID));
			TicketManagerFrame.tmsa.queryStatus(queryObj.toString(), new AsyncCallback<String>() {

				@Override
				public void onFailure(Throwable caught) {
					MessageBox.ins.showError("查询票务状态服务发生错误"+caught.getMessage());
				}

				@SuppressWarnings("deprecation")
				@Override
				public void onSuccess(String result) {
					JSONArray statusArray = (JSONArray) JSONParser.parse(result);
					parent.appendContent(new StatusManager(selectedTicketObj, statusArray, parent));
				}
			
			});
		} else {
			MessageBox.ins.showWarning("请选中一个酒店进行管理");
		}
	}
}
